#include "upload.h"
#include "FreeRtos.h"
#include "semphr.h"
#include "main.h"
#include "cJSON.H"
#include "esp8266.h"
extern SemaphoreHandle_t semhandle;
extern info ttinfo;

void uploaddata()
{
     	xSemaphoreTake(semhandle,portMAX_DELAY);
	    uint16_t light=ttinfo.light;
	    uint8_t tmp=ttinfo.tmp;
	    uint8_t  hum  =ttinfo.hum;
	    xSemaphoreGive(semhandle);

      cJSON*sendObject=cJSON_CreateObject();
	    
       cJSON_AddNumberToObject(sendObject,"light",light);
	     cJSON_AddNumberToObject(sendObject,"tmp",tmp);
	     cJSON_AddNumberToObject(sendObject,"hum",hum);
        
	     char*sendStr=cJSON_Print(sendObject);
	     char realbuffer[512]={0};
			 char*p=sendStr;
			 int i=0;
			 while(*p!='\0')
			 {
			    if(*p=='"'||*p==',')
					{
					   realbuffer[i++]='\\';
						 realbuffer[i++]=*p;
				
					}
					else
					{
					   realbuffer[i++]=*p;
					
					}
			 
			 
			    p++;

			 }
	    esp8266_Publish("upload0001",realbuffer);
	
	    cJSON_Delete(sendObject);
	    vPortFree(sendStr);
}
